<template>
  <div>
    <page-list :x-uri="uri" :x-tabs="tabs" :doactions="doactions" :custom-components="custom" :checkbox-item="false" />
    <sort-dialog :value.sync="sortValue" :visible.sync="visible" :disable-submit="disableSubmit" @submit="onSubmitSort" />
  </div>
</template>

<script>
import PageList from '@public/template/page_list.vue';
import Status from './components/status';
import Icon from './components/icon';
import SortDialog from '@/components/update-sort-dialog';

export default {
  components: {
    PageList,
    SortDialog
  },
  inject: ['reload'],
  data() {
    return {
      uri: '/admin/site/discovery/items',
      tabs: [
        {
          title: '发现页',
          uri: '/site/discovery/items',
          meta: {
            permission: 'site.mobile.discovery.list'
          }
        }
      ],
      custom: {
        status: Status,
        icon: Icon
      },
      doactions: [
        { title: '排序', meta: { permission: 'site.mobile.discovery.sort' }, callback: this.sortPopover },
        { title: '编辑', meta: { permission: 'site.mobile.discovery.edit' }, callback: this.edit }
      ],
      itemId: 0,
      visible: false,
      sortValue: 1, // 排序值
      disableSubmit: false
    };
  },
  methods: {
    edit: function({ row }) {
      this.$router.push({ path: '/site/discovery/items/' + row.id + '/edit' });
    },
    // 排序弹出层
    sortPopover({ row }) {
      this.visible = true;
      this.itemId = row.id
      this.sortValue = row.sort
    },
    // 保存
    onSubmitSort() {
      this.disableSubmit = true;
      this.$http.put('/admin/site/discovery/items/' + this.itemId + '/sort', {
        sort: this.sortValue
      }, ({ msg }) => {
        this.$message.success(msg);
        this.disableSubmit = false;
        this.visible = false;
        this.reload();
      }, ({ msg }) => {
        this.$message.error(msg)
        this.disableSubmit = false;
      }, false)
    }
  }
};
</script>
<style>
.sort-action {
  margin-top: 18px;
}
</style>

